﻿using System;
using System.IO;

using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Filter;
using log4net.Layout;
using log4net.Repository;

namespace ESoft.Core.Logging.Log4Net
{
    /// <summary>
    /// log4net 日志输出适配器
    /// </summary>
    public class Log4NetLoggerAdapter : LoggerAdapterBase
    {
        ILoggerRepository repository;
        /// <summary>
        /// 初始化一个<see cref="Log4NetLoggerAdapter"/>类型的新实例
        /// </summary>
        public Log4NetLoggerAdapter()
        {
            repository = log4net.LogManager.CreateRepository("NETCoreRepository");
            const string fileName = "log4net.config";
            BasicConfigurator.Configure(repository);
            string configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
            if (File.Exists(configFile))
            {
                XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(configFile));
                return;
            }
            //appender.Layout = layout;
        }

        #region Overrides of LoggerAdapterBase

        /// <summary>
        /// 创建指定名称的缓存实例
        /// </summary>
        /// <param name="type">指定名称</param>
        /// <returns></returns>
        protected override ILog CreateLogger(Type type)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(repository.Name, type);
            return new Log4NetLog(log);
        }

        #endregion
    }
}